package com.dzt.cloudreader.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dzt.cloudreader.R;
import com.dzt.cloudreader.adapter.recycler.RecyclerAdapter;
import com.dzt.cloudreader.adapter.recycler.RecyclerHolder;
import com.dzt.cloudreader.model.GankIoDataBean;
import com.dzt.cloudreader.utils.ImgLoadUtil;
import com.dzt.cloudreader.utils.JTimeKit;
import com.dzt.cloudreader.webview.WebViewActivity;

import java.util.List;

/**
 * Created by M02323 on 2017/12/27.
 */

public class AndroidAdapter extends RecyclerAdapter<GankIoDataBean.ResultsBean> {

	private boolean isAll = false;

	public AndroidAdapter(Context context, List<GankIoDataBean.ResultsBean> datas, int layoutId) {
		super(context, datas, layoutId);
	}

	public void setAllType(boolean isAll) {
		this.isAll = isAll;
	}

	public void clear() {
		datas.clear();
	}

	@Override
	public void convert(RecyclerHolder holder, final GankIoDataBean.ResultsBean data, final int position) {
		ImageView ivAllWelfare = holder.findView(R.id.iv_all_welfare);
		LinearLayout llWelfareOther = holder.findView(R.id.ll_welfare_other);
		if (isAll && "福利".equals(data.getType())) {
			ivAllWelfare.setVisibility(View.VISIBLE);
			llWelfareOther.setVisibility(View.GONE);
			ImgLoadUtil.displayEspImage(data.getUrl(), ivAllWelfare, 1);
		} else {
			ivAllWelfare.setVisibility(View.GONE);
			llWelfareOther.setVisibility(View.VISIBLE);
		}
		TextView tvContentType = holder.findView(R.id.tv_content_type);
		if (isAll) {
			tvContentType.setVisibility(View.VISIBLE);
			tvContentType.setText(" · " + data.getType());
		} else {
			tvContentType.setVisibility(View.GONE);
		}
		ImageView ivAndroidPic = holder.findView(R.id.iv_android_pic);
		// 显示gif图片会很耗内存
		if (data.getImages() != null && data.getImages().size() > 0
				&& !TextUtils.isEmpty(data.getImages().get(0))) {
			ivAndroidPic.setVisibility(View.VISIBLE);
			ImgLoadUtil.displayGif(data.getImages().get(0), ivAndroidPic);
		} else {
			ivAndroidPic.setVisibility(View.GONE);
		}
		holder.setText(R.id.tv_android_des, data.getDesc());
		if (TextUtils.isEmpty(data.getWho())) {
			holder.setText(R.id.tv_android_who, R.string.no_name);
		} else {
			holder.setText(R.id.tv_android_who, data.getWho());
		}
		holder.setText(R.id.tv_android_time, JTimeKit.getTranslateTime(data.getPublishedAt()));
		LinearLayout llAll = holder.findView(R.id.ll_all);
		llAll.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				WebViewActivity.loadUrl(v.getContext(), data.getUrl(), "加载中...");
			}
		});
	}
}
